<?php
import('$/util/hash_type.php');

/**
 *
 */
class Cate
{
	/**
	 * Get all by pid
	 * @return array
	 */
	public static function getByPid($pid=0)
	{
		return Db::get(__CLASS__)->select()->where(array(
			'pid'		=>	$pid,
			'off'		=>	0,
			))->order('sort ASC,hid ASC')->hkey('hid')->getAll();
	}
	public static function getList($where)
	{
		return Db::get(__CLASS__)->select()->where($where)->order('sort ASC,hid ASC')->hkey('hid')->getAll();
	}
	
	/**
	 * get a record
	 */
	public static function get($id)
	{
		return intval($id)
			? Db::get(__CLASS__)->select()->where(array(
				'hid'		=>	$id,
				'off'		=>	0,
				))->getOne()
			: false;
	}

	/**
	 * Add new cate for node
	 * @param array $data
	 * @return mixed boolean|int
	 */
	public static function add($data)
	{
		if(empty($data['title']))
		{
			return false;
		}
		
		$pid	=	intval($data['pid']);
		if($pid)
		{
			//用于生成 path
			$ppath	=	Db::get(__CLASS__)->select('path')->where(array('hid'=>$pid))->getCol();
			if(false===$ppath)
			{
				return false;	//父节点好像找不到哦
			}
			
			$data['path']	=	$ppath ? "{$ppath}{$data['pid']}/" : "/{$data['pid']}/";
		}
		if(empty($data['type'])){
			$data['type']	=	NODE_TYPE_CATE;
		}
		
		if(Db::get(__CLASS__)->insert($data)->exec())
		{
			$hid	=	Db::get(__CLASS__)->insertId();
			if($pid){
				Db::get(__CLASS__)->increase('nc', array('hid'=>$pid));
			}
			return $hid;
		}
		else
			return false;
	}
	
	/**
	 * Change cate attr
	 * @return boolean
	 */
	public static function modify($id, $data)
	{
		if(!$id || empty($data['title']))
		{
			return false;
		}
		//不能修改的几个字段
		unset($data['pid'], $data['path'], $data['type']);
		
		return Db::get(__CLASS__)->update($data)->where(array('hid'=>$id))->exec();
	}
	
	/**
	 * 直接删除会有问题，
	 * 	需要检查有没有子类，一并删除，可以使用path
	 * 	需要检查其它使用此类及子类的地方，若有提示不能删除或off
	 */
	public static function delete($id)
	{
		/*
		if(!$id)
		{
			return false;
		}
		if(Db::get(__CLASS__)->select('hid')->where(array('pid'=>$id))->getCol())
		{
			return Db::get(__CLASS__)->update(array('off'=>1))->where(array('hid'=>$id))->exec();
		}
		else
		{
			return Db::get(__CLASS__)->delete()->where(array('hid'=>$id))->exec();
		}
		*/
	}
	
	/** 返回给定cate_id的完整 分类。 取沿途各节点 */
    public function getPathCate($hid)
    {
        $hid    =   intval($hid);
        if(0==$hid) return null;
        $adata      =   Cate::get($hid);
        if($adata)
        {
            if($adata['pid']==0)    return array($adata);
            $road   =   array_diff(explode('/', $adata['path']), array('',null));
            $alist	=	self::getList('hid IN ('.join(',',$road).')', 'hid,title');
            $tmp    =   array();
            foreach($alist as $ar)
            {
                $tmp[$ar['hid']]    =   $ar;
            }
            $alist  =   array();
            foreach($road as $aid){
                $alist[]    =   $tmp[$aid];
            }
            $alist[]        =   $adata;
            return $alist;
        }
        return null;
    }
	
	public static function cachedGetPath($id)
	{
		static $list = array();
		if(isset($list[$id])){
			return $list[$id];
		}
		return $list[$id]=self::getPathCate($id);
	}

    
}

?>